package br.com.mmm.javacenter.framework.view.taglib.helper;

import java.io.IOException;
import java.io.Writer;
import java.util.Calendar;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;

import br.com.mmm.javacenter.framework.view.ContextAttributes;

public class Element  implements Render{

	private JspFragment fragment;
	private JspContext context;
	private Class<?> klass;
	private ServletRequest request;
	private ServletResponse response;
	protected Writer writer;
	public Class<?> getKlass() {
		return klass;
	}
	public void setKlass(Class<?> klass) {
		this.klass = klass;
	}
	public JspFragment getFragment() {
		return fragment;
	}
	public void setFragment(JspFragment fragment) {
		this.fragment = fragment;
	}
	public JspContext getContext() {
		return context;
	}
	public void setContext(JspContext context) {
		this.context = context;
		PageContext pageContext = (PageContext) context;
		this.request = pageContext.getRequest();
		this.response = pageContext.getResponse();
		this.writer = getJspContext().getOut();
	}
	public ServletResponse getResponse() {
		return response;
	}
	public void setResponse(ServletResponse response) {
		this.response = response;
	}
	public ServletRequest getRequest() {
		return request;
	}
	public void setRequest(ServletRequest request) {
		this.request = request;
	}
	public Element(){
		
	}
	public Element(JspContext context,JspFragment fragment){
		this.context = context;
		this.fragment = fragment;
		PageContext pageContext = (PageContext) context;
		this.request = pageContext.getRequest();
		this.response = pageContext.getResponse();
		this.writer = getJspContext().getOut();
	}
	
	public String getAtributo(String nome,Calendar value){
		return getAtributo(nome, String.valueOf(value));
	}
	public String getAtributo(String nome,boolean value){
		return getAtributo(nome, String.valueOf(value));
	}
	public String getAtributo(String nome,long value){
		return getAtributo(nome, String.valueOf(value));
	}
	public String getAtributo(String nome,int value){
		return getAtributo(nome, String.valueOf(value));
	}
	public String getAtributo(String nome,Object value){
		return getAtributo(nome, value==null?null:String.valueOf(value));
	}
	public String getAtributo(String nome,String value){
		return (value!=null?nome+"=\""+value+"\"":"")+' ';
	}
	public void writeBody(Writer writer) throws JspException, IOException{
		if(fragment!= null) fragment.invoke(writer);
	}
	public void setRequestAttribute(String att,Object o){
		context.setAttribute(att,o,PageContext.REQUEST_SCOPE);
	}
	public Object getAttribute(String att){
		return context.getAttribute(att);
	}
	public Object getRequestAttribute(String att){
		return context.getAttribute(att,PageContext.REQUEST_SCOPE);
	}
	public String getContextPath(){
		return (String) getRequestAttribute(ContextAttributes.REQUEST_CONTEXT_PATH);
	}
	public String getContextPath(String complemento){
		return (String) getRequestAttribute(ContextAttributes.REQUEST_CONTEXT_PATH) + complemento;
	}
	public JspContext getJspContext(){
		return context;
	}
	public JspFragment getJspBody(){
		return fragment;
	}
	
	@Override
	public void make() throws JspException, IOException {}
}
